/**
* @param {String} url
* @param {Function} funSuccess
* @param {Function} funComplete
*/
import {
	METHODS
} from "http";

function get(url, funSucces, funComplete) {
	uni.showLoading({
		title: '加载中.....',
		mask: false
	});
	uni.request({
		url: url,
		method: 'GET',
		sslVerify: false,
		header: {
			"content-type": "application/json;charset=UTF-8"
		},
		success: res => {
			funSucces(res.data)
		},
		fail: err => {
			funFail(err)
		},
		complete: () => {
			uni.hideLoading();
			funComplete()
		}
	});
}
/**
 * @param {string} url
 * @param {JSON} header
 * @param {Function} funSuccess
 * @param {Function} fimComplete 
 */
function getWithHeader(url,header,funSuccess,funComplete){
	uni.showLoading({
		title: '加载中.....',
		mask: false
	});
	uni.request({
		url,
		header,
		method: 'GET',
		sslVerify:false,
		success: res => {
			funSuccess(res.data)
		},
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading();
			funComplete()
		}
	});
}

/**
 * @param {string} url
 * @param {JSON} data
 * @param {Function} funSuccess
 * @param {Function} fimComplete 
 */
function postParam(url, data, funSuccess,funComplete){
	uni.showLoading({
		title: '提交数据....',
		mask: false
	});
	uni.request({
		url: url,
		header:{
			"Accept":"application/json; q=0.9, */*; q=0.1",
			"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
		},
		method:'POST',
		sslVerify:false,
		data:data,
		success: res => funSuccess(res.data),
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading()
			funComplete()
		}
	});
}

/**
* @param {string} url
 * @param {JSON} data
 * @param {Function} funSuccess
 * @param {Function} fimComplete 
 */
function post(url, data, funSuccess, funComplete){
 uni.showLoading({
 	title: '提交数据...',
 	mask: false
 });
 uni.request({
 	url,
	method:'POST',
	data:data,
 	success: res => {
		funSuccess(res.data)
		},
 	fail: err => {
		console.log(err);
	},
 	complete: () => {
		uni.hideLoading()
		funComplete()
	}
 });
}

/**
* @param {string} url
 * @param {JSON} data
 * @param {Function} funSuccess
 * @param {Function} fimComplete 
 */
function deleteParams(url, data, funSuccess,funComplete){
	uni.request({
		url: url,
		header:{
			"Accept":"application/json; q=0.9, */*; q=0.1",
			"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
		},
		method:'DELETE',
		sslVerify:false,
		data:data,
		success: res => funSuccess(res.data),
		fail: err => {
			console.log(err);
		},
		complete: () => {
			funComplete()
		}
	});
}
/**
 * POST提交，上传文件到服务器，可同时提交表单数据
 * @param {String} url 服务器接口地址
 * @param {string} name 文件上传接口参数名，一般为file
 *@param {string} filePath 本地文件
 * @param {JSON} data 其他表单参数如user，password等
 * @param {Function} funSuccess 上传成功的回调
 */

function postFile(url, name, filePath, data, funSuccess){
	uni.uploadFile({
		url,
		name,
		filePath,
		formData:data,
		success:res => funSuccess(res)
	})
}

/**
 * 同步的写法实现异步请求，async块内部代码的同步执行的，，会等待await代码的执行结果
 * 如果在lambad表达式内使用，async应该写在匿名表达式里面
 */
function requestAsync(url, data={}, method ='GET'){
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: method,
			data: data,
			success: res => {
				resolve(res)
			},
			fail: err =>{
				reject(err)
			},
			complete: () => {}
		});
	})
}
module.exports = {
	get,
	getWithHeader,
	postParam,
	post,
	deleteParams,
	postFile,
	requestAsync
}